package xm.joincommand;

import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.PlayerDeathEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerResourcePackStatusEvent;
import org.bukkit.event.player.PlayerRespawnEvent;
import org.bukkit.plugin.java.JavaPlugin;

import java.io.File;


public final class Main extends JavaPlugin implements Listener {

    @Override
    public void onEnable() {
        int pluginId = 17227; // <-- Replace with the id of your plugin!
        Metrics metrics = new Metrics(this, pluginId);
        CreateConfig();
        getServer().getPluginManager().registerEvents(this, this);
        getLogger().info("§aJoinCommand插件已加载，作者QQ2279881513");

    }

    @Override
    public void onDisable() {
        getLogger().info("§aJoinCommand插件已卸载，作者QQ2279881513");
    }

    public void CreateConfig() {
        if (!(new File(getDataFolder() + File.separator + "config.yml")).exists()) {
            saveDefaultConfig();
            getLogger().info("插件找不到config.yml配置文件");
        }

        try {
            reloadConfig();


            getLogger().info("插件加载config");
        } catch (Exception e) {
            e.printStackTrace();
            getServer().getPluginManager().disablePlugin(this);
            getLogger().info("插件无法读取config");
        }
    }


    //重生相关
    @EventHandler
    public void onPlayerRespawn(PlayerRespawnEvent event) throws InterruptedException {
        for (String Command : getConfig().getStringList("BeRebornCommands")) {
            Player player = event.getPlayer();
            Command = Command.replace("%player_name%", player.getName()).replace("&","§");
            Bukkit.dispatchCommand(Bukkit.getConsoleSender(), Command);
        }
    }
    //死亡相关
    @EventHandler
    public void onPlayerDeath(PlayerDeathEvent event) throws InterruptedException {
        for (String Command : getConfig().getStringList("DeathCommands")) {
            Player player = event.getEntity();
            Command = Command.replace("%player_name%", player.getName()).replace("&","§");
            Bukkit.dispatchCommand(Bukkit.getConsoleSender(), Command);
        }
    }


    //材质包相关
    @EventHandler
    public void onResourcePackStatus(PlayerResourcePackStatusEvent event) {
        //#资源包成功地下载并应用到了客户端。
        Player player = event.getPlayer();
        if (event.getStatus() == PlayerResourcePackStatusEvent.Status.SUCCESSFULLY_LOADED) {
            for (String Command : getConfig().getStringList("SUCCESSFULLY_LOADEDCommands")) {
                Command = Command.replace("%player_name%", player.getName()).replace("&","§");
                Bukkit.dispatchCommand(Bukkit.getConsoleSender(), Command);
            }
        }
        //#客户端接受了资源包, 但下载失败。
        if (event.getStatus() == PlayerResourcePackStatusEvent.Status.FAILED_DOWNLOAD) {
            for (String Command : getConfig().getStringList("FAILED_DOWNLOADCommands")) {
                Command = Command.replace("%player_name%", player.getName()).replace("&","§");
                Bukkit.dispatchCommand(Bukkit.getConsoleSender(), Command);
            }
        }
        //#客户端拒绝接受资源包。
        if (event.getStatus() == PlayerResourcePackStatusEvent.Status.DECLINED) {
            for (String Command : getConfig().getStringList("DECLINEDCommands")) {
                Command = Command.replace("%player_name%", player.getName()).replace("&","§");
                Bukkit.dispatchCommand(Bukkit.getConsoleSender(), Command);
            }
        }
        //#客户端接受了资源包, 并开始下载。
        if (event.getStatus() == PlayerResourcePackStatusEvent.Status.ACCEPTED) {
            for (String Command : getConfig().getStringList("ACCEPTEDCommands")) {
                Command = Command.replace("%player_name%", player.getName()).replace("&","§");
                Bukkit.dispatchCommand(Bukkit.getConsoleSender(), Command);
            }
        }
    }


    //进服相关
    @EventHandler
    public void onJoin(PlayerJoinEvent event) {
        Player player = event.getPlayer();
        if (!player.hasPlayedBefore()) {      //判断玩家是否进入过服务器(检测世界文件的)
            for (String Command : getConfig().getStringList("Command")) {
                Command = Command.replace("%player_name%", player.getName()).replace("&","§");
                Bukkit.dispatchCommand(Bukkit.getConsoleSender(), Command);
            }
        } else {
            for (String EnterCommands : getConfig().getStringList("EnterCommands")) {
                EnterCommands = EnterCommands.replace("%player_name%", player.getName()).replace("&","§");
                Bukkit.dispatchCommand(Bukkit.getConsoleSender(), EnterCommands);
            }
        }

    }



    //指令相关
    @Override
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
        if (command.getName().equalsIgnoreCase("xm")) {
            if (args.length == 0) {
                sender.sendMessage("/xm reload 重载配置文件");
                return true;
            }
            if (args[0].equalsIgnoreCase("reload")) {
                CreateConfig();
                sender.sendMessage("重载成功");
                return true;
            }
        }
        return false;
    }

}
